package com.ws.ssmzongjie3.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.ssmzongjie3.bean.Acl_User;
import com.ws.ssmzongjie3.bean.Business;
import com.ws.ssmzongjie3.bean.ProVo;
import com.ws.ssmzongjie3.bean.Route;
import com.ws.ssmzongjie3.service.Acl_UserService;
import com.ws.ssmzongjie3.service.BusinessService;
import com.ws.ssmzongjie3.service.RouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/shangjia")
public class BusinessController {
    @Autowired
    private BusinessService businessService;
    @Autowired
    private RouteService routeService;
    @Autowired
    private Acl_UserService acl_userService;
    @RequestMapping("/list")
    public String select(Model model, ProVo proVo, Business business){
        System.out.println("links:"+business);
        PageHelper pageHelper = new PageHelper();//分页帮助类
        pageHelper.startPage(proVo.getCurrentPage(),proVo.getPageSize());//分页   每页条数
        List<Business> businesses = businessService.listBusiness(business);
        PageInfo pageInfo = new PageInfo(businesses);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("business",business);

        return "/WEB-INF/admin/business/list";
    }

    @RequestMapping("/huixian")
    public String huixian(Model model, Business business){
        Business business1 = businessService.getBusinessById(business);
        model.addAttribute("business",business1);
        return "/WEB-INF/admin/business/update";
    }


    @RequestMapping("/delete")
    public String delete(Business business, Model model) {
        Route route = new Route();
        Acl_User aclUser = new Acl_User();
        route.setBusiness_id(business.getBusiness_id());
        aclUser.setSid(business.getBusiness_id());


        List<Route> routes = routeService.listRoute(route);
        acl_userService.listUser(aclUser);
        if (routes.isEmpty()){
            if (acl_userService.listUser(aclUser).isEmpty()){
                businessService.deleteBusiness(business);
            }
        }
        return "redirect:/shangjia/list";
    }

    @RequestMapping("/insert")
    public String insert(Business business) {
        businessService.insertBusiness(business);
        return "redirect:/shangjia/list";
    }

    @RequestMapping("/add")
    public String add(Business business, Model model) {
        return "/WEB-INF/admin/business/add";
    }
    @RequestMapping("/update")
    public String update(Business business, Model model) {
        businessService.updateBusiness(business);
        return "redirect:/shangjia/list";
    }
}